package com.sptus.web.quartz.job;
import com.sptus.common.utils.StringUtils;
import com.sptus.system.domain.DevInfoMsg6;
import com.sptus.system.service.IDevInfoMsg6Service;
import com.sptus.web.service.CloudIEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class SptBatteryNumber  {
    @Autowired
    CloudIEquipmentService equipmentService;

    @Autowired
    IDevInfoMsg6Service devInfoMsg6Service;
    public int getBatteryNum(String value){
        if(StringUtils.isBlank(value)){
            return 0;
        }
        int num=0;
        String[] split = value.split(";");
        for (String s : split) {
            boolean b = Integer.parseInt(s) > 0;
            if(b){
                num=1;
                break;
            }

        }
        return  num;
    }

    //电池数量更新
    @Scheduled(cron = "0 0/1 * * * ? ")
    //cron修改为 1分钟
    public void run() throws JobExecutionException {
        List<DevInfoMsg6> devInfoMsg6s = devInfoMsg6Service.getBaseMapper().selectList(null);
        for (DevInfoMsg6 devInfoMsg6 : devInfoMsg6s) {
            int Bn=0;
            Bn+= getBatteryNum(devInfoMsg6.getBalone1());
            Bn+= getBatteryNum(devInfoMsg6.getBalone2());
            Bn+= getBatteryNum(devInfoMsg6.getBalone3());
            Bn+= getBatteryNum(devInfoMsg6.getBalone4());
            Bn+= getBatteryNum(devInfoMsg6.getBalone5());
            Bn+= getBatteryNum(devInfoMsg6.getBalone6());
            Bn+= getBatteryNum(devInfoMsg6.getBalone7());
            Bn+= getBatteryNum(devInfoMsg6.getBalone8());
            devInfoMsg6.setBatteryNumber(Bn+"");
        }
        devInfoMsg6Service.updateBatchById(devInfoMsg6s);

    }
}
